package com.media.themovieworld.api;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpEntity;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ApiRequest {

//	public static String BASE_URL = "http://117.120.31.83:7001/Service/MobileServicePort";
	public static String BASE_URL = "http://117.120.31.83:7001/service/mobile";
	public static String genBodyRequest(HashMap<String, String> param) {
		String body = null;
		Set mapSet = (Set) param.entrySet();
		Iterator mapIterator = mapSet.iterator();
		while (mapIterator.hasNext()) {
			Map.Entry mapEntry = (Map.Entry) mapIterator.next();
			String key = (String) mapEntry.getKey();
			String value = (String) mapEntry.getValue();
			body = body + "<" + key + ">" + value + "</" + key + ">";
		}
		return body;
	}

	public static String sendRequest(HashMap<String, String> param, String method) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		String responseString;
		String response = "";
		String xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:json=\"http://JsonUtils/\">"
				+ "<soapenv:Header/>" + "<soapenv:Body>" + "<json:" + method
				+ ">";
		String xmlFootter = "</json:" + method + ">" + "</soapenv:Body>"
				+ "</soapenv:Envelope>";
		try {
			HttpPost httppost = null;
			if(method.equals("getListAdvertise")){
				httppost = new HttpPost("http://vipmobi.net:7001/service/mobile");
			}else{
				httppost = new HttpPost(BASE_URL);
			}
			httppost.setHeader("SOAPAction", "");
			httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
			String strEnvelope = xmlHeader + genBodyRequest(param) + xmlFootter;
			HttpEntity entity = new StringEntity(strEnvelope, HTTP.UTF_8);
			httppost.setEntity(entity);
			ResponseHandler<String> strResponseHandler = new BasicResponseHandler();
			responseString = httpClient.execute(httppost, strResponseHandler);
			InputStream stream = new ByteArrayInputStream(responseString.getBytes("UTF-8"));
			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document doc = builder.parse(stream);
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName("return");
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					response = node.getTextContent();
				}
			}			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return response;
	}
}
